using Circus.CodeEditor;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;
namespace Circus.Syntax.CodeCompletion
{
	public class CodeSnippetsProvider : CodeCompletionProvider, ICodeSnippetsProvider, ICodeCompletionProvider, IList<IComparable>, ICollection<IComparable>, IEnumerable<IComparable>, IEnumerable
	{
		private ICodeSnippetMember parent;
		public override int ColumnCount
		{
			get
			{
				return 1;
			}
		}
		public override string EditPath
		{
			get
			{
				return (this.parent != null) ? this.parent.EditPath : string.Empty;
			}
		}
		public override ImageList Images
		{
			get
			{
				return (base.Images != null) ? base.Images : ((this.GetParent() != null) ? this.GetParent().Images : null);
			}
			set
			{
				base.Images = value;
			}
		}
		public CodeSnippetsProvider()
		{
			this.ShowDescriptions = true;
			this.UseIndent = false;
			this.EditField = StringConsts.CodeSnippetsEditField;
		}
		public CodeSnippetsProvider(ICodeSnippetMember parent) : this()
		{
			this.parent = parent;
		}
		public virtual CodeSnippet FindByShortcut(string shortcut, bool caseSensitive)
		{
			CodeSnippet result;
			foreach (object current in this)
			{
				if (current is CodeSnippet)
				{
					CodeSnippet codeSnippet = (CodeSnippet)current;
					if (string.Compare(shortcut, codeSnippet.Header.Shortcut, !caseSensitive) == 0)
					{
						result = codeSnippet;
						return result;
					}
				}
				else
				{
					if (current is ICodeSnippetMember)
					{
						ICodeSnippetMember codeSnippetMember = (ICodeSnippetMember)current;
						CodeSnippet codeSnippet = codeSnippetMember.Snippets.FindByShortcut(shortcut, caseSensitive);
						if (codeSnippet != null)
						{
							result = codeSnippet;
							return result;
						}
						codeSnippet = codeSnippetMember.Members.FindByShortcut(shortcut, caseSensitive);
						if (codeSnippet != null)
						{
							result = codeSnippet;
							return result;
						}
					}
				}
			}
			result = null;
			return result;
		}
		public virtual bool ShouldIndent(CodeSnippet snippet)
		{
			return this.UseIndent;
		}
		public override void OnClosePopup(object sender, ClosingEventArgs e)
		{
			if (e.Accepted && this.SelIndex >= 0 && this.SelIndex < base.Count)
			{
				object obj = base[this.SelIndex];
				if (obj is ICodeSnippetMember)
				{
					e.Provider = ((ICodeSnippetMember)obj).SnippetsAndMembers;
					e.Handled = (e.Provider.Count > 0);
				}
			}
			base.OnClosePopup(sender, e);
		}
		public override string GetDescription(int index)
		{
			object obj = base[index];
			return (obj is CodeSnippet) ? ((CodeSnippet)obj).Description : string.Empty;
		}
		public override int GetImageIndex(int index)
		{
			object obj = base[index];
			int result;
			if (obj is CodeSnippet)
			{
				result = ((CodeSnippet)obj).ImageIndex;
			}
			else
			{
				if (obj is ICodeSnippetMember)
				{
					result = ((ICodeSnippetMember)obj).ImageIndex;
				}
				else
				{
					result = -1;
				}
			}
			return result;
		}
		public override string GetColumnText(int index, int column)
		{
			return (column == 0) ? this.GetName(index) : string.Empty;
		}
		public override string GetText(int index)
		{
			object obj = base[index];
			return (obj is CodeSnippet) ? ((CodeSnippet)obj).Code.Code : string.Empty;
		}
		public override string GetName(int index)
		{
			object obj = base[index];
			string result;
			if (obj is CodeSnippet)
			{
				result = ((CodeSnippet)obj).Header.Title;
			}
			else
			{
				if (obj is ICodeSnippetMember)
				{
					result = ((ICodeSnippetMember)obj).Name;
				}
				else
				{
					result = string.Empty;
				}
			}
			return result;
		}
		public override ICodeCompletionProvider GetParent()
		{
			return (this.parent != null) ? this.parent.Parent : null;
		}
		public override bool ColumnVisible(int column)
		{
			return true;
		}
	}
}
